var chatRemote = require('../remote/chatRemote');

module.exports = function(app) {
    return new Handler(app);
};

var Handler = function(app) {
    this.app = app;
};

var handler = Handler.prototype;

handler.mysend = function (msg,session,next) {
    var rid = session.get('rid');
    var username = session.uid.split('*')[0];
    var channelService = this.app.get('channelService');
    //更新玩家position
    var pos = msg.position;
    session.set('position',pos);
    //返回其他玩家的position
    var channel = channelService.getChannel(rid, false);
    var users = channel.getMembers();//用户列表
    var userlist = null ;
    var poslist = null;
    var sessionService = self.app.get('sessionService');
    sessionService.getByUid()
    for(var i = 0; i < users.length; i++){
        userlist = userlist+users[i].uid+',';
        poslist = poslist+channel.getMember(users[i].uid)['sid'].pos+',';
    }
    //发送消息
    var param = {
        userlist:userlist,
        poslist:poslist,
        user:username,
    }
    var tuid = session.uid;
    var tsid = channel.getMember(tuid)['sid'];
    channelService.pushMessageByUids('onGame',param,[{
        uid: tuid,
        sid: tsid
    }]);
    next(null, {
        route: msg.route
    });
}


// handler.send = function(msg, session, next) {
//     var rid = session.get('rid');
//     var username = session.uid.split('*')[0];
//     var channelService = this.app.get('channelService');
//     var param = {
//         msg: msg.content,
//         from: username,
//         target: msg.target
//     };
//     channel = channelService.getChannel(rid, false);

//     //the target is all users
//     if(msg.target == '*') {
//         channel.pushMessage('onGame', param);
//     }
//     //the target is specific user
//     else {
//         var tuid = msg.target + '*' + rid;
//         var tsid = channel.getMember(tuid)['sid'];
//         channelService.pushMessageByUids('onGame', param, [{
//             uid: tuid,
//             sid: tsid
//         }]);
//     }
//     next(null, {
//         route: msg.route
//     });
// };